﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Common;
using OneMile.Entity.Model;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
    [MobileAPI(Code = "MC0401", Name = "用户获取抵用券信息（分页）")]
    public class UGetCouponListByUserId : BaseAPI, IProcessMessage
    {
        private BLLUCoupon bllUCoupon;
        public BLLUCoupon BllUCoupon
        {
            get
            {
                if (bllUCoupon == null)
                    bllUCoupon = BLLFactory.getInstance().CreateUCouponBLL();
                return bllUCoupon;
            }
        }


        MobileRspMsg IProcessMessage.ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                string UserId = reqMsg.Body.GetFieldValue("UserId");
                int PageNo = reqMsg.Head.PageNo;
                int PageSize = reqMsg.Head.PageSize;
                if (String.IsNullOrEmpty(UserId) || PageNo == 0 || PageSize == 0)
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }
                List<ECoupon> list = (List<ECoupon>)BllUCoupon.GetCouponListByUserId(UserId, PageNo, PageSize);

                for (int i = 0; i < list.Count; i++)
                {
                    var fll = rspMsg.Body.AddFieldList("Coupon");
                    fll.AddField("CouponId", list[i].CouponId.TString());
                    fll.AddField("CouponPrice", list[i].CouponPrice.TString());
                    fll.AddField("CouponName", list[i].CouponName.TString());
                    fll.AddField("StartDate", list[i].StartDate.TString());
                    fll.AddField("EndDate", list[i].EndDate.TString());
                    fll.AddField("IsUsed", list[i].IsUsed.TString());
                    fll.AddField("UsedTime", list[i].UsedTime.ToString());
                }

                rspMsg.Head.PageNo = PageNo;
                rspMsg.Head.PageSize = PageSize;

                rspMsg.Head.RspCode = MessageCode.Success.Code;
                rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("GetCouponListByUserId" + e);
                throw e;
            }
        }
    }
}